<template>
  <view class="register-container">
    <!-- 背景图轮播 -->
    <view class="background-slider">
      <image 
        v-for="(bg, index) in backgrounds" 
        :key="index"
        :src="url('g', index , 'png')"
        class="background-image"
        :class="{
          'active': currentBgIndex === index,
          'fade-out': isChanging && currentBgIndex === index,
          'fade-in': isChanging && nextBgIndex === index
        }"
      />
    </view>
    
    <!-- 遮罩层 -->
    <view class="overlay"></view>

    <!-- 注册卡片 -->
    <view class="register-card animate__animated animate__fadeIn">
      <view class="register-header">
        <text class="register-title">{{ status.data.message }}</text>
      </view>

      <view class="register-form">
        <uni-forms ref="form" :modelValue="formData">
          <uni-forms-item name="account">
            <uni-easyinput
              v-model="formData.account"
              placeholder="手机号"
              :clearable="true"
              prefixIcon="phone"
            />
          </uni-forms-item>

          <uni-forms-item name="password">
            <uni-easyinput
              v-model="formData.password"
              type="password"
              placeholder="密码"
              :clearable="true"
              prefixIcon="locked"
            />
          </uni-forms-item>

          <uni-forms-item name="confirm_password">
            <uni-easyinput
              v-model="formData.confirm_password"
              type="password"
              placeholder="确认密码"
              :clearable="true"
              prefixIcon="locked"
            />
          </uni-forms-item>

          <uni-forms-item name="gender" label="性别">
            <uni-data-select
              v-model="formData.gender"
              :localdata="genderOptions"
              placeholder="请选择性别"
            />
          </uni-forms-item>

          <uni-forms-item name="resource" label="本校生">
            <uni-data-checkbox
              v-model="formData.resource"
              :localdata="schoolOptions"
            />
          </uni-forms-item>

          <view class="btn-group">
            <button class="btn-register" @tap="create">注册</button>
            <button class="btn-cancel" @tap="clear">取消</button>
          </view>
          
          <!-- 添加管理员测试按钮 -->
          <view class="admin-test">
            <button class="btn-test" @tap="fillTestData">
              <text class="bi bi-gear"></text>
              测试账号
            </button>
          </view>
        </uni-forms>
      </view>

      <view class="login-link">
        <text>已有账号？</text>
        <text class="link-text" @tap="goToLogin">去登录!</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { reactive, ref, onMounted, onUnmounted } from "vue";

const formData = reactive({
  account: "", // 手机号
  password: "",
  confirm_password: "",
  gender: "保密",
  resource: "",
});

const status = ref({ data: {message:'填写注册信息'} });

const genderOptions = [
  { value: '男', text: '男' },
  { value: '女', text: '女' },
  { value: '保密', text: '保密' }
];

const schoolOptions = [
  { value: '是', text: '是' },
  { value: '否', text: '否' }
];

const url = $url;

// 背景图相关
const backgrounds = [1, 2, 3]; // 对应三张背景图
const currentBgIndex = ref(0);
const nextBgIndex = ref(0);
const isChanging = ref(false);
let bgTimer = null;

// 切换背景图
const changeBg = () => {
  isChanging.value = true;
  nextBgIndex.value = (currentBgIndex.value + 1) % backgrounds.length;
  
  setTimeout(() => {
    currentBgIndex.value = nextBgIndex.value;
    isChanging.value = false;
  }, 1000);
};

// 启动背景轮播
onMounted(() => {
  bgTimer = setInterval(changeBg, 5000);
});

// 清理定时器
onUnmounted(() => {
  if (bgTimer) {
    clearInterval(bgTimer);
  }
});

const clear = () => {
  uni.showToast({
    title: '已取消注册',
    icon: 'none'
  });
  formData.account = '';
  formData.password = '';
  formData.confirm_password = '';
  formData.gender = '保密';
  formData.resource = '';
};

const create = async () => {
  try {
    // 表单验证
    if (!formData.account || !formData.password || !formData.confirm_password) {
      uni.showToast({
        title: '请填写完整信息',
        icon: 'none'
      });
      return;
    }

    // 密码确认验证
    if (formData.password !== formData.confirm_password) {
      uni.showToast({
        title: '两次密码输入不一致',
        icon: 'none'
      });
      return;
    }

    // 手机号格式验证
    if (!/^1[3-9]\d{9}$/.test(formData.account)) {
      uni.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      });
      return;
    }

    const res = await uni.request({
      url: "https://fc-mp-d7d620be-76ea-43bb-a05a-d38ceb13c655.next.bspapp.com/register",
      method: "POST",
      data: {
        account: formData.account,
        password: formData.password,
        gender: formData.gender || "保密"
      }
    });

    handleRegisterResponse(res);
    console.log(res);
    
  } catch (error) {
    console.log('注册错误:', error);
    uni.showToast({
      title: '注册失败',
      icon: 'error'
    });
  }
};

// 处理注册响应
const handleRegisterResponse = (res) => {
  const { status, message } = res.data;
  
  switch (status) {
    case 0:
      uni.showToast({
        title: '注册成功',
        icon: 'success',
        success: () => {
          setTimeout(() => {
            uni.navigateTo({
              url: '/pages/other/login'
            });
          }, 1500);
        }
      });
      break;
    case 1:
      uni.showToast({
        title: '账号已存在',
        icon: 'error'
      });
      break;
    default:
      uni.showToast({
        title: message || '注册失败',
        icon: 'error'
      });
  }
};

const goToLogin = () => {
  uni.navigateTo({
    url: '/pages/other/login'
  });
};

// 添加测试数据填充方法
const fillTestData = () => {
  formData.account = "13800138000";  // 测试手机号
  formData.password = "123456";       // 测试密码
  formData.confirm_password = "123456";
  formData.gender = "保密";
  formData.resource = "是";
  
  uni.showToast({
    title: '测试数据已填充',
    icon: 'success'
  });
};
</script>

<style lang="scss" scoped>
.register-container {
  min-height: 100vh;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.background-slider {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 1s ease-in-out;
  
  &.active {
    opacity: 1;
  }
  
  &.fade-out {
    opacity: 0;
  }
  
  &.fade-in {
    opacity: 1;
  }
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
}

.register-card {
  position: relative;
  width: 100%;
  max-width: 600rpx;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 20rpx;
  padding: 40rpx;
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.2);
  margin: 20rpx;
  
  .register-title {
    color: var(--td-brand-color);
    text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
  }
}

.register-header {
  text-align: center;
  margin-bottom: 40rpx;
}

.btn-group {
  display: flex;
  gap: 20rpx;
  margin-top: 40rpx;
}

.btn-register, .btn-cancel {
  flex: 1;
  height: 80rpx;
  line-height: 80rpx;
  border-radius: 40rpx;
  font-size: 32rpx;
  border: none;
  box-shadow: 0 4rpx 12rpx rgba(152, 31, 35, 0.3);
  
  &:active {
    transform: translateY(2rpx);
    box-shadow: 0 2rpx 6rpx rgba(152, 31, 35, 0.3);
  }
}

.btn-register {
  background: var(--td-brand-color);
  color: #fff;
}

.btn-cancel {
  background: #f5f5f5;
  color: #666;
}

.login-link {
  text-align: center;
  margin-top: 40rpx;
  font-size: 28rpx;
  
  .link-text {
    color: var(--td-brand-color);
    font-weight: bold;
    margin-left: 10rpx;
  }
}

// 添加动画类
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.animate__fadeIn {
  animation: fadeIn 0.8s ease-out;
}

// 表单项样式优化
:deep(.uni-forms-item) {
  margin-bottom: 20rpx;
  
  .uni-forms-item__label {
    color: #333;
    font-size: 28rpx;
  }
  
  .uni-easyinput__content,
  .uni-data-select {
    background: rgba(255, 255, 255, 0.8);
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 8rpx;
    
    &:focus-within {
      border-color: var(--td-brand-color);
      box-shadow: 0 0 0 2px rgba(152, 31, 35, 0.1);
    }
  }
}

:deep(.uni-data-checkbox) {
  .uni-data-checkbox-item {
    margin-right: 20rpx;
  }
}

.register-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10rpx;
  width: 100%;
  height: 80rpx;
  line-height: 80rpx;
  background: var(--td-brand-color, #0052d9);
  color: #fff;
  border-radius: 40rpx;
  font-size: 32rpx;
  margin-top: 40rpx;
  border: none;

  &:active {
    opacity: 0.8;
  }
}

.admin-test {
  margin-top: 20rpx;
  padding: 0 20rpx;
  
  .btn-test {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10rpx;
    width: 100%;
    height: 70rpx;
    line-height: 70rpx;
    background: #666;
    color: #fff;
    border-radius: 35rpx;
    font-size: 28rpx;
    border: none;
    opacity: 0.7;
    
    .bi {
      font-size: 32rpx;
    }
    
    &:active {
      opacity: 0.9;
    }
  }
}
</style>
